package com.daocao.hua.view;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daocao.hua.R;
import com.daocao.hua.activity.EmailIdentityActivity;
import com.daocao.hua.activity.LevelRuleActivity;
import com.daocao.hua.activity.V3EmailIdentityActivity;
import com.daocao.hua.adapter.OtherIdentityAdapter;
import com.daocao.hua.api.bean.OtherIdentityBean;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.AlertModel;
import com.daocao.hua.util.DisplayUtil;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.Util;

import org.sufficientlysecure.htmltextview.HtmlTextView;

import java.util.Calendar;
import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by xsooy-pc on 2017/8/21.
 */

public class DialogQueue {
    public static android.app.Dialog showDefaultDialog(Context context, String title, String content,
                                                       String leftButton, String rightButton, boolean showCloseBtn,
                                                       final DialogDefaultClickListener dialogClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.default_dialog, null);
        dialog.setContentView(view);
        LinearLayout titleView = (LinearLayout) view.findViewById(R.id.title_view);
        TextView mTitle = (TextView) view.findViewById(R.id.title);
        TextView mContext = (TextView) view.findViewById(R.id.content);
        TextView mLeftBtn = (TextView) view.findViewById(R.id.left_button);
        TextView mClose = (TextView) view.findViewById(R.id.close);
        if (showCloseBtn) mClose.setVisibility(View.VISIBLE);
        else mClose.setVisibility(View.INVISIBLE);

        //标题
        if (TextUtils.isEmpty(title))
            mTitle.setVisibility(View.GONE);
        else {
            titleView.setVisibility(View.VISIBLE);
            mTitle.setText(title);
        }
        //内容
        if (TextUtils.isEmpty(content)) {
            mContext.setVisibility(View.GONE);
            ((View) view.findViewById(R.id.line)).setVisibility(View.VISIBLE);
        } else {
            mContext.setVisibility(View.VISIBLE);
            mContext.setText(content);
        }

        //左边按钮
        if (TextUtils.isEmpty(leftButton)) {
            mLeftBtn.setVisibility(View.GONE);
            ((View) view.findViewById(R.id.d_line)).setVisibility(View.GONE);
        } else {
            mLeftBtn.setVisibility(View.VISIBLE);
            mLeftBtn.setText(leftButton);
        }
        ((TextView) view.findViewById(R.id.right_button)).setText(rightButton);
        dialog.setCancelable(false);
        view.findViewById(R.id.left_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialogClickListener.cancel();
                            }
                        }, 200);
                    }
                });
        view.findViewById(R.id.right_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialogClickListener.confirm();
                            }
                        }, 200);
                    }
                });
        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 8;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }


    public static android.app.Dialog showInviteDialog(Context context, Spanned text) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_invite, null);
        ((TextView) view.findViewById(R.id.txt_tips)).setText(text);
        ((TextView) view.findViewById(R.id.txt_tips)).setMovementMethod(ScrollingMovementMethod.getInstance());


        dialog.setContentView(view);

        dialog.setCancelable(true);
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 8;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog showRegisterTipDialog(Context context) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_register_tip, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        view.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
//        Glide.with(context).load(R.mipmap.img_register_tips).
//                bitmapTransform(new RoundedCornersTransformation(context, DisplayUtil.dip2px(context, 5), 0, RoundedCornersTransformation.CornerType.TOP)).
//                crossFade(1000).
//                into((ImageView) view.findViewById(R.id.imageView));
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();

        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog gotoEmail(Context context) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_goto_email__tip, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Util.startActivity(context, V3EmailIdentityActivity.class);
            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();

        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog showFriendlyTipDialog(Context context) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_friendly_tip, null);
        dialog.setContentView(view);
        view.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 9;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog showLevelUpDialog(Context context, String edu) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_level_up, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        ImageView imgLevel = (ImageView) view.findViewById(R.id.img_level);
        int level = PreferenceUtils.getInt(context, Const.KEY.LEVEL, 0);
        switch (level) {
            case 1:
                imgLevel.setImageResource(R.mipmap.dialog_lv1);
                break;
            case 2:
                imgLevel.setImageResource(R.mipmap.dialog_lv2);
                break;
            case 3:
                imgLevel.setImageResource(R.mipmap.dialog_lv3);
                break;
            case 4:
                imgLevel.setImageResource(R.mipmap.dialog_lv4);
                break;
            case 5:
                imgLevel.setImageResource(R.mipmap.dialog_lv5);
                break;
            case 6:
                imgLevel.setImageResource(R.mipmap.dialog_lv6);
                break;
        }
        ((TextView) view.findViewById(R.id.txt_level_tips)).setText("你已经是LV" + level + "啦");
        ((TextView) view.findViewById(R.id.txt_edu_tips)).setText(edu + "元额度到手");
        view.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.btn_rule).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Util.startActivity(context, LevelRuleActivity.class);
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 9;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog showTimeDialog(Context context) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_sign, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        TimeView timeview = (TimeView) view.findViewById(R.id.time_view);
        Calendar calendar = Calendar.getInstance();
        long time = calendar.getTimeInMillis();
        timeview.setTime(time, 20, new Integer[]{1, 3, 8, 10, 20}, 20);
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        mWindow.setGravity(Gravity.CENTER_VERTICAL);
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        int heightDialog = Util.getDisplayMetrics(context).heightPixels;
//        lp.y = (int) (heightDialog * 0.2);
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.height = (int) (heightDialog);
        mWindow.setAttributes(lp);


        return dialog;
    }

    public interface DialogDefaultClickListener {
        public abstract void confirm();

        public abstract void cancel();
    }

    public static android.app.Dialog gotoTongxunlu(Context context, DialogDefaultClickListener dialogDefaultClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_tongxunlu, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDefaultClickListener.cancel();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDefaultClickListener.cancel();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.btn_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDefaultClickListener.confirm();
                dialog.dismiss();
            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();

        mWindow.setAttributes(lp);
        return dialog;
    }


    public static android.app.Dialog gotoXuQi(Context context, String content) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_v3_xuqi, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        view.findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        TextView txtTips = (TextView) view.findViewById(R.id.txt_tips);
        txtTips.setText(content);

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 9;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog goHuankuan(Context context, String content) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_v3_huankuan, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

//        ((HtmlTextView) view.findViewById(R.id.txt_content)).setHtml(content);
        FrameLayout mWebContainer = (FrameLayout) view.findViewById(R.id.webview);
        WebView mWebView = new WebView(context.getApplicationContext());
        mWebContainer.addView(mWebView);
        mWebContainer.setVisibility(View.VISIBLE);
        mWebView.setBackgroundColor(0);

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        mWebView.loadDataWithBaseURL(null, content + "", "text/html", "utf-8", null);


        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 9;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog showMoneyArriveDialog(Context context,
                                                           final DialogDefaultClickListener dialogClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_extension_money_arrive, null);
        dialog.setContentView(view);


        dialog.setCancelable(false);
        view.findViewById(R.id.left_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialogClickListener.cancel();
                            }
                        }, 200);
                    }
                });
        view.findViewById(R.id.right_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialogClickListener.confirm();
                            }
                        }, 200);
                    }
                });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 8;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }

    public static android.app.Dialog goGongxi(Context context, String content) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_v3_gongxi, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dialog.dismiss();
            }
        });

        FrameLayout mWebContainer = (FrameLayout) view.findViewById(R.id.webview);
        WebView mWebView = new WebView(context.getApplicationContext());
        mWebContainer.addView(mWebView);
        mWebContainer.setVisibility(View.VISIBLE);
        mWebView.setBackgroundColor(0);

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        mWebView.loadDataWithBaseURL(null, content + "", "text/html", "utf-8", null);
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 9;
        }
        mWindow.setAttributes(lp);
        return dialog;
    }


    public static android.app.Dialog gotoOtherIdentity(Context context, DialogDefaultClickListener dialogDefaultClickListener, List<OtherIdentityBean> data) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_other_identity, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
        OtherIdentityAdapter adapter = new OtherIdentityAdapter(context);
        list.setAdapter(adapter);

        adapter.update(data);
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDefaultClickListener.cancel();
                dialog.dismiss();
            }
        });

        view.findViewById(R.id.btn_success).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDefaultClickListener.confirm();
                dialog.dismiss();
            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();

        mWindow.setAttributes(lp);
        return dialog;
    }
}
